package com.dahua.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dahua.R;
import com.dahua.bean.ScanResult;
import com.dahua.ui.main.ScanPrepareUploadAct;

import java.util.List;


/**
 */
public class CreatedPeopleAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ScanResult> nearbyInfoList;
    private Context context;

    public CreatedPeopleAdapter(Context context, List<ScanResult> nearbyInfoList) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.nearbyInfoList = nearbyInfoList;
    }

    public void setNearbyInfoList(List<ScanResult> nearbyInfoList) {
        this.nearbyInfoList = nearbyInfoList;
    }

    @Override
    public int getCount() {
        return nearbyInfoList == null ? 0 : nearbyInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return nearbyInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
            convertView = inflater.inflate(R.layout.create_people_list, parent, false);
            holder = new ViewHolder();
            holder.txtTitle = (TextView) convertView.findViewById(R.id.item);
            holder.extratxt = (TextView) convertView.findViewById(R.id.textView1);
            holder.tvError = (TextView) convertView.findViewById(R.id.tv_error);
            holder.ivDel = (ImageView) convertView.findViewById(R.id.iv_del);
            holder.textCode = (TextView) convertView.findViewById(R.id.textCode);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ScanResult nearbyInfo = nearbyInfoList.get(position);

        holder.txtTitle.setText(nearbyInfo.getContent());

        if(!TextUtils.isEmpty(nearbyInfo.getModel()) && !nearbyInfo.getModel().equals("null")){
            holder.textCode.setText(nearbyInfo.getModel());
        }else{
            holder.textCode.setText("");
        }
        holder.extratxt.setText(nearbyInfo.getTime());
        holder.tvError.setText(nearbyInfo.getError());
        holder.ivDel.setTag(position);
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (Integer) v.getTag();
                ((ScanPrepareUploadAct)context).del(nearbyInfo, position);
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        ImageView imageView,ivDel;
        TextView txtTitle, extratxt,tvError,textCode;
    }


}
